package com.example.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.model.dtos.PageResult;
import com.example.model.dtos.R;
import com.example.model.pojo.*;
import com.example.service.DepartmentService;
import com.example.service.InFormService;
import com.example.service.MaterialsService;
import com.example.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 入库控制类
 */
@RestController
@RequestMapping("inWarehouse/inForm/")
public class InFormController {

    @Autowired(required = false)
    MaterialsService materialsService;

    @Autowired(required = false)
    SupplierService supplierService;

    @Autowired(required = false)
    InFormService inFormService;
    /**
     * 获取所有物资信息
     */
    @RequestMapping("getAllMaterials")
    public R<List<Materials>> getAllMaterials(){
        List<Materials> materialsList=materialsService.getAll();
        return R.success(materialsList);
    }

    /**
     * 获取所有供应商信息
     */
    @RequestMapping("getAllSupplier")
    public R<List<Supplier>> getAllSupplier(){
        List<Supplier> supplierList=supplierService.getAll();
        return R.success(supplierList);
    }

    /**
     * 提交入库申请
     */
    @RequestMapping("submit")
    public R<String> submitInForm(@RequestBody Map<String,Object> map){
        LinkedHashMap<String,Object> linkedHashMap = (LinkedHashMap) map.get("inForm");
        InForm inForm =new InForm(null, (String) linkedHashMap.get("supplierName"), (String)linkedHashMap.get("principal"), (String)linkedHashMap.get("operator"), Double.valueOf(String.valueOf( linkedHashMap.get("aggregate"))), new Date(), "0");

        List<LinkedHashMap<String,Object>> inFormDetailListJSON = (List<LinkedHashMap<String, Object>>) map.get("inFormDetailList");
        ArrayList<InFormDetail> inFormDetailList = new ArrayList<>();
        for (LinkedHashMap<String, Object> LinkedHashMapD : inFormDetailListJSON) {
            InFormDetail inFormDetail = new InFormDetail(null, null, (String) LinkedHashMapD.get("materialsId"), (String) LinkedHashMapD.get("materialsName"), Double.valueOf(String.valueOf( LinkedHashMapD.get("price"))), (Integer) LinkedHashMapD.get("amount"));
            inFormDetailList.add(inFormDetail);
        }

        Boolean flag = inFormService.submit(inForm,inFormDetailList);

        if (!flag){
            return R.error("提交入库单失败");
        }

        return R.success("提交入库单成功");
    }

    /**
     * 采购单详情
     */
    @RequestMapping("details")
    public R<List<InFormDetail>> Details(@RequestBody Map<String,String> map){
        String myId = map.get("myId");

        List<InFormDetail> details = inFormService.details(myId);
        return R.success(details);
    }


    /**
     * 获取待审核表单
     */
    @RequestMapping("getUnreviewedPage")
    public R<PageResult> getUnreviewedPage(@RequestBody Map<String,String> map){
        String pageStr = map.get("current");
        String pageSizeStr = map.get("size");
        String inFormId = map.get("inFormId");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");

//        若为空，则查询第一页，每页10条
        if (StringUtils.isBlank(pageStr)){
            pageStr="1";
        }
        if (StringUtils.isBlank(pageSizeStr)){
            pageSizeStr="10";
        }
        Integer page = Integer.parseInt(pageStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);



        PageResult pageResult = inFormService.getUnreviewedPage(page,pageSize,inFormId,startDate,endDate);
        return R.success(pageResult);
    }

    /**
     * 提交审核结果
     */
    @RequestMapping("audit")
    public R<String> audit(@RequestBody Map<String,String> map){
        String myId = map.get("myId");
        String status = map.get("status");
        InForm inForm = new InForm(myId, null, null, null, null, null, status);

        Boolean flag = inFormService.audit(inForm);
        if (!flag){
            return R.error("审核失败");
        }

        return R.success("审核完成");
    }
}
